/**
 * 成功回掉事件
 * @param data
 */
function successCallback(data){

    if(data.success){
        layer.msg(data.message,{time:1000,icon:1},function () {
            WindowManager.closeLayer({
                returnValue:''
            });
        });

    }else{
        layer.msg(data.message,{time:1000,icon:2});
    }

}

/**
 * 使用layer 打开一个页面
 * @param title
 * @param content
 * @param area
 */
function openLayer(title,content,area) {

    if(area===undefined || area===null){
        area=['1150px', '480px'];
    }
    WindowManager.openLayer({
        type: 2,
        title: title,
        area: area,
        content: content,
        callBack:function(data){//窗口关闭后的回调函数
            $('#dataList').datagrid('reload');
        }
    });

}
/**
 * 删除按钮操作
 * @param id id
 * @param modelPath 模块地址
 */
function initDeleteClick(modelPath,id) {
    layer.confirm('是否确定删除当前选中数据？', {
        btn: ['确定','取消'] //按钮
    }, function(){
        $.ajax({
            type: "POST",
            url: modelPath+'delete.shtml',
            data: {id:id,delState:1},
            dataType: "json",
            success: function(data){
                if(data.success){
                    layer.msg(data.message,{time:1000,icon:1},function () {
                        $('#dataList').datagrid('reload');
                    });
                }else{
                    layer.msg(data.message,{time:1000,icon:2});
                }
            }
        });
    });
}
/**
 * 表单提交
 * @param url
 */
function initSaveClick(url) {

    $('#btn_save').on('click',function () {

        var index=layer.confirm('是否确定保存？', {
            btn: ['确定','取消'] //按钮
        }, function(){
            $('#submit_form').form('submit', {
                url: url,
                onSubmit: function (param) {
                    var isValid = $(this).form('validate');
                    if (!isValid) {
                        layer.close(index);
                        layer.msg("请正确填写表单")
                    }
                    return $(this).form('validate');
                },
                success: function (result) {
                    var data = $.parseJSON(result);
                    successCallback(data)
                }
            });
        });

    });
}
/**
 * 查询按钮事件
 */
function initSearchClick(sort) {

    if(sort===undefined || sort ===null){
        sort='d.addTime';
    }
    //查询按钮事件
    $('#btn_search').on('click',function () {

        var $applyList=$('#dataList');
        //获得查询form的参数
        var queryParams=$('#queryForm').serializeJson();
        queryParams["sort"]=sort;
        queryParams["order"]="desc";
        $applyList.datagrid('options').queryParams = queryParams;
        $applyList.datagrid('reload');


    });
}
//高级查询点击事件
function initAdvancedSearch() {
    $('.advanced-search').on('click',function () {
        var advancedForm=$(this).nextAll('.advanced-form');
        advancedForm.toggle();
        $('#dataList').datagrid('resize');
    })
}
//清空按钮事件
function  initResetClick() {
    $('#btn_search_reset').on('click',function () {
        $('#queryForm').form('reset');
    });
}
//表单页面reset事件
function  initFormReset() {
    $('#btn_reset').on('click',function () {
        $('#submit_form').form('reset');
    });
}

//重置下拉表格
function resetCombogrid(id) {

    $(id).combogrid({
        required:false,
        readonly:true,
        prompt:'请选择',
        data:[],
        url:null
    });
}

//重置下拉框
function resetCombobox(id) {

    $(id).combobox({
        required:true,
        editable:false,
        prompt:'请选择',
        data:[],
        url:null
    });
}
function initYears() {
    var date=new Date;
    var year=date.getFullYear();
    $('#years').combobox({
        data:[
            {"id":year,"text":year},
            {"id":year-1,"text":year-1},
            {"id":year-2,"text":year-1},
            {"id":year-3,"text":year-3},
            {"id":year-4,"text":year-4}
        ],
        valueField:'id',
        textField:'text',
        editable:false,
        panelHeight:'auto'
    });
}
function initMonths() {

    var data=[];
    for (var i=1;i<=12;i++){
        var t=i;
        var d={};
        if(i<10){
            t='0'+i;
        }
        d["id"]=t;
        d["text"]=t;
        data.push(d)
    }
    $('#months').combobox({
        data:data,
        valueField:'id',
        textField:'text',
        editable:false
    });
}